import {
    getToken,
    setRefreshToken,
    setToken,
    setRoles
} from '@/utils/auth'
import {
    appletLogin
} from '@/api/index'
const user = {
    state: {
        token: getToken(),
        // baseurl: ' http://lgznb.natapp1.cc',
        baseurl: 'https://getewayentry.yingxuzdqp.com',
        // baseurl: 'http://192.168.1.3:20000',
    },
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
    },

    actions: {
        // 登录
        userLogin({ commit }, info) {
            console.log(info)
            let code = info
            return new Promise((resolve, reject) => {
                appletLogin({code}).then(res=>{
                    if(res.statusCode == '00000'){
                        console.log('2222222222')
                        console.log(res)
                        setToken(res.data.access_token)
                        commit('SET_TOKEN', res.data.access_token)
                        setRefreshToken(res.data.refresh_token)
                        if(res.data.roles.length){
                            setRoles(res.data.roles)
                        }else{
                            setRoles('')
                        }
                    }
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },
    }
}

export default user
